<template>
	<view class="bind-login-wrap">
		<view class="top-wrap">
			<u--image src="/static/logo-1.png" width="200" height="60" customStyle="margin:0 auto;"></u--image>
		</view>

		<view class="main-wrap">
			<u--text size="18" type="info" bold margin="20rpx 0" text="账号绑定登录"></u--text>
			<u--form :model="loginForm" :rules="rules" ref="form">
				<u-form-item prop="username" borderBottom>
					<u--input v-model="loginForm.username" clearable border="none" placeholder="请输入用户名"
						prefixIcon="account-fill"
						prefixIconStyle="font-size: 44rpx; color: #3c9cff; margin-right: 10rpx"
						:clearable="true"></u--input>
				</u-form-item>
				<u-form-item prop="password" borderBottom>
					<u--input v-model="loginForm.password" type="password" clearable border="none" placeholder="请输入密码"
						prefixIcon="lock-fill" prefixIconStyle="font-size: 44rpx; color: #3c9cff; margin-right: 10rpx"
						:clearable="true"></u--input>
				</u-form-item>
				<u-form-item prop="code" borderBottom>
					<!-- 注意：由于兼容性差异，如果需要使用前后插槽，nvue下需使用u--input，非nvue下需使用u-input -->
					<u-input placeholder="请输入验证码" v-model="loginForm.code" border="none" prefixIcon="integral-fill"
						prefixIconStyle="font-size: 44rpx; color: #3c9cff; margin-right: 10rpx">
						<template slot="suffix">
							<u--image :src="codeUrl" width="120px" height="30px" @click="getCode"></u--image>
						</template>
					</u-input>
				</u-form-item>
				<view style="margin-top:20px;"><u-button type="primary" text="绑 定" @click="handleBindlogin"></u-button>
				</view>
			</u--form>
			<view class="register-wrap">
				<view class="now-btn">
					<u--text text="注册绑定" type="primary" @click="bindRegister"></u--text>
				</view>
			</view>
			<view class="footer-wrap">
				<view class="clause">
					<checkbox-group @change="handleClauseCheckbox">
						<label>
							<checkbox :checked="isClause" style="transform:scale(0.6)" />
						</label>
						<text class="check_text">
							<text>已阅读并同意</text>
							<text class="service" @click="handleService">服务协议</text>
							<text>与</text>
							<text class="privacy" @click="handlePrivacy">隐私政策</text>
						</text>
					</checkbox-group>
				</view>
				<view class="copyright"><text>Copyright © 2022-2023 FastBee All Rights Reserved.</text></view>
			</view>
		</view>

		<view class="other-wrap">
			<u-modal :show="isRegister" title="提示" content='账户不存在，您是否要注册新账户？' cancelText="暂不注册" confirmText="确认注册"
				:showCancelButton="true" @cancel="isRegister = false" @confirm="confirmRegister"></u-modal>
		</view>
	</view>
</template>

<script>
	import projectConfig from '@/env.config.js';
	import { getProfile } from '@/apis/modules/common.js'

	export default {
		data () {
			return {
				token: '',
				codeUrl: '',
				isClause: false,
				loginForm: {
					username: '',
					password: '',
					rememberMe: false,
					code: '',
					uuid: '',
					bindId: ''
				},
				rules: {
					username: {
						type: 'string',
						min: 2,
						max: 20,
						required: true,
						message: '请输入用户名',
						trigger: ['blur', 'change']
					},
					password: {
						type: 'string',
						required: true,
						min: 5,
						max: 20,
						message: '请输入密码',
						trigger: ['blur', 'change']
					},
					code: {
						type: 'integer',
						required: true,
						message: '请输入验证码',
						trigger: ['blur', 'change']
					},
				},
				isRegister: false,
			};
		},
		onLoad (option) {
			this.loginForm.bindId = option.bindId;
			this.getCode();
		},
		methods: {
			//微信登录
			handleBindlogin () {
				this.$refs.form.validate().then(res => {
					if (!this.isClause) {
						uni.showToast({
							icon: 'none',
							title: '请阅读并勾选协议'
						});
						return;
					}
					// 调用绑定登录
					this.$api.common.bindLogin(this.loginForm).then(async res => {
						if (res.code === 200) {
							// 存储token和账号
							this.saveToken(res.token);
							this.saveAccount();
							// 获取用户信息
							let profile = await this.getProfile();
							uni.$u.vuex('profile', profile);
							// 跳转主页
							uni.switchTab({
								url: '/pages/tabBar/home/index'
							});
						} else if (res.code === 450) {
							this.isRegister = true;
						} else {
							if (res.msg) {
								uni.showToast({
									icon: "none",
									title: res.msg,
									complete: (res) => {
										setTimeout(() => {
											this.getCode();
										}, 1500);
									}
								})
							}
						}
					})
				}).catch(errors => {
					uni.$u.toast("账号不能为空");
				});
			},
			// 获取验证码
			getCode () {
				this.$api.common.captchaImage().then(res => {
						this.codeUrl = 'data:image/gif;base64,' + res.img;
						this.loginForm.uuid = res.uuid;
						this.loginForm.code = "";
					})
					.catch(err => {
						this.$u.toast(err.msg);
					});
			},
			// 用户绑定注册
			bindRegister () {
				uni.$u.route('/pages/login/bindRegister', { bindId: this.loginForm.bindId });
			},
			// 获取用户信息
			getProfile () {
				return new Promise((resolve, reject) => {
					getProfile().then(res => {
						resolve(res.data);
					}).catch(err => {
						this.$u.toast(err.msg);
					})
				});
			},
			saveToken (token) {
				// 本地缓存存储token
				uni.setStorageSync('token', token);
				// vuex存储token
				uni.$u.vuex('vuex_token', token);
			},
			saveAccount () {
				// 本地缓存存储
				uni.setStorageSync('username', this.loginForm.username);
				uni.setStorageSync('password', this.loginForm.password);
			},
			// 服务协议
			handleService () {
				let title = '服务协议';
				let url = projectConfig.officialWebUrl + 'service-agreement.html';
				uni.navigateTo({
					url: `/pages/common/webview/index?title=${title}&url=${url}`
				});
			},
			// 隐私政策
			handlePrivacy () {
				let title = '隐私政策';
				let url = projectConfig.officialWebUrl + 'privacy-policy.html';
				uni.navigateTo({
					url: `/pages/common/webview/index?title=${title}&url=${url}`
				})
			},
			// 勾选协议
			handleClauseCheckbox (e) {
				this.isClause = !this.isClause;
			},
			// 注册确认
			confirmRegister () {
				this.isRegister = false;
				uni.$u.route('/pages/login/bindRegister', {
					bindId: this.loginForm.bindId
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #007aff;
		height: 100%;
	}

	.bind-login-wrap {
		height: 100%;

		.top-wrap {
			padding-top: 92rpx;
			height: 420rpx;
		}

		.main-wrap {
			position: fixed;
			padding: 40rpx;
			background: #ffffff;
			border-top-left-radius: 0;
			border-top-right-radius: 0;
			top: 420rpx;
			left: 0;
			right: 0;
			bottom: 0;

			.register-wrap {
				margin-top: 30rpx;
				display: flex;
				flex-direction: initial;
				justify-content: flex-end;

				.now-btn {
					margin-left: 20rpx;
				}
			}

			.footer-wrap {
				position: fixed;
				bottom: 40rpx;
				left: 0;
				right: 0;
				color: #909399;
				text-align: center;

				.other-login-wrap {
					margin-bottom: 60rpx;
				}

				.copyright {
					font-size: 16rpx;
					margin-top: 20rpx;
				}

				.clause {
					font-size: 28rpx;

					.check_text {
						display: inline-block;
						vertical-align: middle;
					}

					.service,
					.privacy {
						color: #3c9cff;
					}
				}
			}
		}
	}
</style>